layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;
    var  user;

    if (top !== window){   //  登陆页面出现在 iframe
        top.location.href = window.location.href;
    }



    //登录按钮
    form.on("submit(login)",function(data){

        var user = {
            "account" : $("#userName").val(),  //登录名
            "password" : $("#password").val(),
            "loginType":"pc"              //表示 web端登陆
        };

        $.ajax({
            url:"user/login",
            type: "POST",
            data:   JSON.stringify(user),
            contentType: "application/json;charset=utf-8",

            success: function(res){
                if(res.code != 200){ // 登录失败
                    layer.msg(res.msg);

                }else {


                    var  userName =  res.data.userName;
                    var roleName = res.data.roleName;
                    var photo = res.data.photo;
                    window.sessionStorage.setItem("userName",userName);
                    window.sessionStorage.setItem("roleName",roleName);
                    window.sessionStorage.setItem("password",res.data.password);
                    window.sessionStorage.setItem("photo",photo);
                    // window.location.href = "../index.html";
                    location.href = "/index.html";

                }
            }});

        //  $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        //   $.post("user/login2",{
        //       account : $("#userName").val(),  //登录名
        //       password : $("#password").val()  //密码
        //
        //   } ,function(res){
        //
        //       if(res.code == -1){ // 登录失败
        //           layer.msg(res.msg);
        //
        //       }else {
        //
        //           var  userName = 1;// res.data.userName;
        //           var roleName =2;// res.data.role.name;
        //           window.sessionStorage.setItem("userName",userName);
        //           window.sessionStorage.setItem("roleName",roleName);
        //           window.location.href = "/index.html";
        //
        //       }
        //   })

        return false;
    });

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
